package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tiantianyuegou.R;
import com.tiantianyuegou.model.CouponModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 优惠券的adapter
 * Created by ivany on 2016/1/13.
 */
public class SelectCouponAdapter extends BaseAdapter {
    private Context mContext;
    private List<CouponModel> mData;

    public SelectCouponAdapter(Context mContext, List<CouponModel> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    public SelectCouponAdapter(Context mContext) {
        this.mContext = mContext;
        mData=new ArrayList<>();

    }

    public void setData(List<CouponModel> data) {
        mData.clear();
        mData.addAll(data);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_select_coupon, parent, false);
            holder = new ViewHolder();
            holder.mTxt_value = (TextView) convertView.findViewById(R.id.item_coupon_value);
            holder.mTxt_condition = (TextView) convertView.findViewById(R.id.item_coupon_condition);
            holder.mTxt_name = (TextView) convertView.findViewById(R.id.item_coupon_name);
            holder.mTxt_number = (TextView) convertView.findViewById(R.id.item_coupon_number);
            holder.mTxt_startDate = (TextView) convertView.findViewById(R.id.item_coupon_startDate);
            holder.mTxt_endData = (TextView) convertView.findViewById(R.id.item_coupon_endDate);
            holder.mImg_select = (ImageView) convertView.findViewById(R.id.item_coupon_select);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        CouponModel model = mData.get(position);
        holder.mTxt_value.setText(model.getValue());
        holder.mTxt_condition.setText("满"+model.getCondition()+"减"+model.getValue());
        holder.mTxt_name.setText(model.getName());
        holder.mTxt_number.setText(model.getNumber());
        holder.mTxt_startDate.setText(model.getStartDate());
        holder.mTxt_endData.setText(model.getEndDate());
        if (model.isSelect()) {
            holder.mImg_select.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.select_icon));
        } else {
            holder.mImg_select.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.unselect_icon));
        }
        return convertView;
    }

    class ViewHolder {
        TextView mTxt_value;
        TextView mTxt_condition;
        TextView mTxt_name;
        TextView mTxt_number;
        TextView mTxt_startDate;
        TextView mTxt_endData;
        ImageView mImg_select;
    }
}
